﻿using System.Collections;
using System.Collections.Generic;
using UnityEngine;

public class Monster : MonoBehaviour,IRuseable
{
    public float m_hp;
    float m_maxHp ;
    public float m_speed;
    List<Vector3> roadPath;
    Vector3 pos;
    Transform m_hpPos;
    float time;
    public float Hp
    {
        get { return m_hp; }
    }
    public float MaxHp
    {
        get { return m_maxHp; }
    }
    public Vector3 Hp_Pos
    {
        get { return m_hpPos.position; }
    }
    // Use this for initialization
    void Start () {
        
    }
	
	// Update is called once per frame
	void Update () {
        if (m_hp<=0)
        {
            Spawner.monsters.Remove(this.gameObject);
            Pools.Instance.Unspawn(this.gameObject);
            return;
        }
        
    }

    float scale;
    public void Onspawn()
    {
        MonsterIfo info = StaticData.Instance.MonstersInfo[Consts.MonsterType];
        m_maxHp = info.hp;
        m_speed = info.speed;
        m_hp = m_maxHp;
        roadPath =new List<Vector3>( Spawner.mapRoads);
        m_hpPos = transform.Find("hp_pos");
        pos = roadPath[0];
        transform.localPosition = roadPath[0];
        roadPath.Remove(roadPath[0]);
        time = Mathf.Abs(roadPath[0].x - pos.x) / m_speed;
        pos.x = roadPath[0].x;
        if(Consts.MonsterType>1)
        {
            transform.localScale *= Consts.MonsterType;
            scale = Consts.MonsterType;
        }

        StopAllCoroutines();
        StartCoroutine(Walking());
    }

    public void OnUnspawn()
    {
        roadPath = null;
        transform.localScale = new Vector3(1, 1, 1);
    }
    public static void Attackting()
    {
        UIBattle.bloods--;
    }
    public void Attacktted()
    {
        m_hp--;
    }
    IEnumerator Walking()
    {
        while(true)
        {
            if (roadPath.Count > 0 && UIBattle.bloods > 0)
            {

                if (transform.localPosition.x == roadPath[0].x && pos.z != roadPath[0].z)
                {
                    time = Mathf.Abs(roadPath[0].z - pos.z) / m_speed;
                    pos = roadPath[0];
                }
                transform.LookAt(pos);
                transform.localPosition = Vector3.MoveTowards(transform.localPosition, pos, Time.deltaTime * m_speed);
                if (Vector3.Distance(transform.localPosition, roadPath[0]) < 0.001)
                {
                    transform.localPosition = pos;
                    roadPath.Remove(roadPath[0]);
                    if (roadPath.Count > 0)
                    {
                        time = Mathf.Abs(roadPath[0].x - pos.x) / m_speed;
                        pos.x = roadPath[0].x;
                    }
                    else
                    {
                        Pools.Instance.Unspawn(this.gameObject);
                        Spawner.monsters.Remove(this.gameObject);
                        Attackting();
                    }
                }
            }
            yield return 0;
        }        
    }
}
